require "#{__dir__}/helper.rb"

describe 'PaylerAPI' do
  subject { PaylerAPI }
  before  { VCR.insert_cassette name }
  after   { VCR.eject_cassette }
  let(:card_params) { { card_number: '5555555555555599',
                        expired_year: '19',
                        expired_month: '12',
                        secure_code: '123' } }

  describe '#pay' do
    let :params do
      card_params.merge order_id: 'test_pay_order_01',
                        amount: '30000'
    end

    it 'should send true request' do
      subject.pay(params).ok?.must_equal true
    end

    it 'should return duplicate order_id error' do
      subject.pay(params).duplicate_order_id?.must_equal true
    end
  end

  describe 'block-charge' do
    let :params do
      { order_id: 'test_block_order_05',
          amount: '30000' }.merge(card_params)
    end

    describe '#block' do
      it 'should blocks the specified amount card' do
        subject.block(params).ok?.must_equal true
      end
    end

    describe '#charge' do
      it 'should charge blocked order' do
        subject.charge(params).ok?.must_equal true
      end
    end

    #describe '#refund' do
      #it 'should refund order' do
        #subject.charge(params).ok?.must_equal true
      #end
    #end
  end


  describe '#get_status' do
    it 'should return data by order_id' do
      resp = subject.get_status order_id: 'test_pay_order_01'
      resp.ok?.must_equal true
      resp.data["amount"].must_equal 30000
    end

    it 'should return error by wrong order_id' do
      resp = subject.get_status order_id: 'incorrect_order'
      resp.error?.must_equal true
    end
  end

  describe '#send_3ds' do
    it 'should send 3ds continue request' do
      resp = subject.send_3ds md: 'test_md', pares: 'test_pares'
      resp.ok?.must_equal true
    end
  end

  # TODO:
  # describe('#refund', '#retrieve)

  describe '#configure' do
    before { PaylerAPI.configure {|config| config.access_key = 'foo-bar' } }
    after  { PaylerAPI.configuration.access_key = 'test-key' }

    it 'should setup new access_key' do
      PaylerAPI.configuration.access_key.must_equal 'foo-bar'
    end
  end

end
